package cn.com.inossem.operation;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Parcelable;
import cn.com.inossem.config.WSConfig;
import cn.com.inossem.db.DatabaseHelper;
import cn.com.inossem.model.SkillsInfoBean;
import cn.com.inossem.network.NetworkConnect;
import cn.com.inossem.requestmanager.WooRequestFactory;

import com.foxykeep.datadroid.exception.ConnectionException;
import com.foxykeep.datadroid.exception.CustomRequestException;
import com.foxykeep.datadroid.exception.DataException;
import com.foxykeep.datadroid.requestmanager.Request;
import com.foxykeep.datadroid.service.RequestService.Operation;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class SkillsOperation implements Operation {
	public static Bundle bundle;

	Context content = null;

	@Override
	public Bundle execute(Context arg0, Request request)
			throws ConnectionException, DataException, CustomRequestException {
		content = arg0;

		NetworkConnect connection = new NetworkConnect(WSConfig.LOCAL_URL
				+ "userInfo/findUserSkill");
		connection.setPostText("{\"username\":\"" + "" + "\",\"password\":\""
				+ "" + "\"}");
		System.out.println("{\"username\":\"" + "" + "\",\"password\":\"" + ""
				+ "\"}");
		String body = connection.execute();

		System.out.println("SkillsOperation:" + body);
		bundle = new Bundle();
		try {
			JSONObject jsonObject = new JSONObject(body);
			int errorFlag = jsonObject.getInt("errorFlag");

			List<SkillsInfoBean> retList;
			if (errorFlag == WSConfig.RESULT_SUCESS) {

				Gson datagson = new Gson();
				String listUserSkill = jsonObject.getString("listUserSkill");
				retList = datagson.fromJson(listUserSkill,
						new TypeToken<List<SkillsInfoBean>>() {
						}.getType());

			} else {
				retList = null;
			}
			// 返回返回值
			bundle.putParcelableArrayList(
					WooRequestFactory.RESPONSE_SKILLS_DATA,
					(ArrayList<? extends Parcelable>) retList);
			// 设置结果
			bundle.putInt(WooRequestFactory.BUNDLE_EXTRA_SKILLSSUCCESS,
					errorFlag);
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return bundle;
	}

	public void charu(String tabname, ContentValues contentValues,
			String whereClause) {
		DatabaseHelper dh = new DatabaseHelper(content);
		SQLiteDatabase db = dh.getWritableDatabase();

		db.update(tabname, contentValues, whereClause, null);

		db.close();
		dh.close();
	}
}
